//
//  IMETabBarBaseController.m
//  IMDemo
//
//  Created by tqh on 2019/7/5.
//  Copyright © 2019 tqh. All rights reserved.
//

#import "IMETabBarBaseController.h"
#import <objc/runtime.h>
#import "IMEBaseController.h"
#import "IMENavBaseController.h"
#import "IMETabBar.h"

@interface IMETabBarBaseController ()<UINavigationControllerDelegate>

@end

@implementation IMETabBarBaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initControl];
    [self setupTabbar];
    [self customTabBar];
    [self setSelectedIndex:1];
    [self setSelectedIndex:0];
}

- (void)initControl {
    
    NSArray *array = [self mainItemArray];
    [self classNameArray:array];
}

- (NSArray *)mainItemArray {
    return @[@"ViewController",@"IMEFriendListController",@"IMESessionListController",@"ViewController"];
}

-(void)setupTabbar
{
    IMETabBar *tabBar = [[IMETabBar alloc] init];
    [self setValue:tabBar forKeyPath:@"tabBar"];
}

- (void)customTabBar {
    
//    [[UITabBar appearance] setBackgroundImage:[UIImage yy_imageWithColor:[UIColor clearColor]]];
//    [[UITabBar appearance] setBackgroundColor:[UIColor clearColor]];
    //@"tapbar_top_line"
    [[UITabBar appearance] setShadowImage:[UIImage new]];
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"b8b8b8"];
    normalAttrs[NSFontAttributeName] = [IMEFont PingFangSC_SemiboldWithSize:10];
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#2296F3"];
    selectedAttrs[NSFontAttributeName] = [IMEFont PingFangSC_SemiboldWithSize:10];
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}

//创建一些基本控制器
- (void)classNameArray:(NSArray *)array{
    NSArray *titleArray = @[@"说说",@"好友",@"消息",@"我的"];
    NSArray *imageArray = @[@"home_normal",@"find_normal",@"news_normal",@"me_normal"];
    NSArray *selectedArray = @[@"home_selected",@"find_selected",@"news_selected",@"me_selected"];
    NSMutableArray *controlArray = [NSMutableArray array];
    for (NSString *string in array) {
        const char *className = [string cStringUsingEncoding:NSASCIIStringEncoding];
        Class newClass = objc_getClass(className);
        if (!newClass) {
            //创建一个类
            Class superClass = [NSObject class];
            newClass = objc_allocateClassPair(superClass, className, 0);
            //注册你创建的这个类
            objc_registerClassPair(newClass);
        }
        [controlArray addObject:newClass];
    }
    int i = 0;
    NSMutableArray *array1 = [NSMutableArray array];
    for (Class class in controlArray) {
        IMEBaseController *vc = [class new];
        
        vc.title = titleArray[i];
        vc.tabBarItem.image = [[UIImage imageNamed:imageArray[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedArray[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        IMENavBaseController *nav = [[IMENavBaseController alloc]initWithRootViewController:vc];
        nav.delegate = self;
        [array1 addObject:nav];
        i++;
    }
    [self setViewControllers:array1 animated:YES];
}

- (BOOL)classNameArray:(NSArray *)array viewController:(UIViewController *)viewController{
    BOOL reslut = YES;
    for (NSString *string in array) {
        const char *className = [string cStringUsingEncoding:NSASCIIStringEncoding];
        Class newClass = objc_getClass(className);
        if ([viewController isKindOfClass:newClass]) {
            reslut = NO;
        }
    }
    return reslut;
}

#pragma mark - UINavigationControllerDelegate
//切换TABBar也可以
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    //需要隐藏导航栏的控制器填入名字即可
    BOOL hidden = ![self classNameArray:@[] viewController:viewController];
    [viewController.navigationController setNavigationBarHidden:hidden animated:YES];
}

//处理一级界面卡死问题
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (navigationController.viewControllers.count == 1) {
        navigationController.interactivePopGestureRecognizer.enabled = NO;
        navigationController.interactivePopGestureRecognizer.delegate = nil;
    }else {
        if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            navigationController.interactivePopGestureRecognizer.enabled = NO;
        }
    }
}

@end
